package com.woniuxy.oldmanapi.controller;

import com.woniuxy.oldmanapi.entity.Caretype;
import com.woniuxy.oldmanapi.service.CareTypeService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CareTypeController {
    @Autowired
    CareTypeService careTypeService;

    //查询全部护理项目类别
    @GetMapping("/careType/alltype")
    public ResponseResult<List<Caretype>> alllist(){
        ResponseResult<List<Caretype>> result=new ResponseResult<>();
        try {
           List<Caretype> list= careTypeService.findAll();
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(list);
        }catch (Exception e){
            result.setMsg("查询失败");
            result.setCode(201);
        }
        return  result;
    }

    //添加护理项目类别
    @PostMapping("/careType/addtype")
    public ResponseResult<Void> add(@RequestBody Caretype caretype){
        ResponseResult<Void> result=new ResponseResult<>();
        try {
           careTypeService.addType(caretype);
            result.setMsg("添加成功");
            result.setCode(200);

        }catch (Exception e){
            result.setMsg("添加失败");
            result.setCode(201);
        }
        return  result;
    }
    //编辑护理项目类别
    @PostMapping("/careType/modifytype")
    public ResponseResult<Void> modifytype(@RequestBody Caretype caretype){
        ResponseResult<Void> result=new ResponseResult<>();
        try {
            careTypeService.modifyType(caretype);
            result.setMsg("修改成功");
            result.setCode(200);

        }catch (Exception e){
            result.setMsg("修改失败");
            result.setCode(201);
        }
        return  result;
    }

    //删除护理项目类别--提示将删除相关项目和护理级别
    @GetMapping("/careType/removetype/{id}")
    public ResponseResult<Void> removeType(@PathVariable("id") int id){
        ResponseResult<Void> result=new ResponseResult<>();
        try {
            careTypeService.removeType(id);
            result.setMsg("删除成功");
            result.setCode(200);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setCode(201);
        }
        return  result;
    }
}

